import { defineConfig } from 'vite'
import react from '@vitejs/plugin-react'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [react()],
  server: {
    port: 3000,
    proxy: {
      '/api': {
        target: 'http://localhost:8088',
        changeOrigin: true,
        secure: false,
        // 确保转发所有头部，包括Authorization
        configure: (proxy, options) => {
          proxy.on('proxyReq', (proxyReq, req, res) => {
            // 转发所有原始请求头
            Object.keys(req.headers).forEach(key => {
              if (req.headers[key]) {
                proxyReq.setHeader(key, req.headers[key]);
              }
            });
            console.log('代理请求:', req.method, req.url, '\n头部:', req.headers.authorization ? '有Authorization' : '无Authorization');
          });
          proxy.on('proxyRes', (proxyRes, req, res) => {
            console.log('代理响应:', proxyRes.statusCode, req.url);
          });
        },
      },
    },
  },
  build: {
    outDir: 'dist',
    sourcemap: true,
  },
})